/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.nifi.processors.huawei.obs.model;

import com.obs.services.model.ObjectMetadata;
import com.obs.services.model.VersionOrDeleteMarker;
import org.apache.nifi.processor.util.list.ListableEntity;
import org.apache.nifi.processors.huawei.obs.OBSUtils;
import org.apache.nifi.serialization.SimpleRecordSchema;
import org.apache.nifi.serialization.record.*;
import org.apache.nifi.serialization.record.Record;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.nifi.processors.huawei.obs.Constants.*;

public class OBSRecord  implements Comparable<OBSRecord>, Serializable, ListableEntity {
    private static final long serialVersionUID = 1L;
    private static final RecordSchema SCHEMA;
    private String region;

    private String endpoint;

    private VersionOrDeleteMarker versionSummary;

    private ObjectMetadata objectMetadata;

    static {
        final List<RecordField> fields = new ArrayList<>();
        fields.add(new RecordField(OBS_OBJECT, RecordFieldType.STRING.getDataType(), false));
        fields.add(new RecordField(OBS_BUCKET, RecordFieldType.STRING.getDataType(), false));
        fields.add(new RecordField(OBS_OWNER, RecordFieldType.STRING.getDataType(), true));
        fields.add(new RecordField(OBS_E_TAG, RecordFieldType.STRING.getDataType(), false));
        fields.add(new RecordField(OBS_LAST_MODIFIED, RecordFieldType.TIMESTAMP.getDataType(), false));
        fields.add(new RecordField(OBS_LENGTH, RecordFieldType.LONG.getDataType(), false));
        fields.add(new RecordField(OBS_STORAGE_CLASS, RecordFieldType.STRING.getDataType(), false));
        fields.add(new RecordField(OBS_IS_LATEST, RecordFieldType.BOOLEAN.getDataType(), false));
        fields.add(new RecordField(OBS_VERSION, RecordFieldType.STRING.getDataType(), true));
        fields.add(new RecordField(OBS_USER_META, RecordFieldType.MAP.getMapDataType(RecordFieldType.STRING.getDataType()), true));
        SCHEMA = new SimpleRecordSchema(fields);
    }

    @Override
    public Record toRecord() {
        final Map<String, Object> values = new HashMap<>();
        values.put(OBS_OBJECT, versionSummary.getKey());
        values.put(OBS_BUCKET, versionSummary.getBucketName());
        if (versionSummary.getOwner() != null) {
            values.put(OBS_OWNER, versionSummary.getOwner().getId());
        }
        values.put(OBS_E_TAG, versionSummary.getEtag());
        values.put(OBS_LAST_MODIFIED, new Timestamp(versionSummary.getLastModified().getTime()));
        values.put(OBS_LENGTH, versionSummary.getSize());
        values.put(OBS_STORAGE_CLASS, versionSummary.getStorageClass());
        values.put(OBS_IS_LATEST, versionSummary.isLatest());
        final String versionId = versionSummary.getVersionId();
        if (versionId != null && !versionId.equals(NULL_VERSION_ID)) {
            values.put(OBS_VERSION, versionSummary.getVersionId());
        }
        if (objectMetadata != null) {
            values.put(OBS_USER_META, objectMetadata.getAllMetadata());
        }
        return new MapRecord(SCHEMA, values);
    }

    @Override
    public int compareTo(OBSRecord o) {
        return 0;
    }

    @Override
    public String getName() {
        if (versionSummary == null) {
            return null;
        }
        return versionSummary.getObjectKey();
    }

    @Override
    public String getIdentifier() {
        if (versionSummary == null) {
            return null;
        }
        return OBSUtils.getUrl(region, endpoint, versionSummary.getBucketName(), versionSummary.getObjectKey());
    }

    @Override
    public long getTimestamp() {
        if (versionSummary == null) {
            return 0;
        }
        return versionSummary.getLastModified().getTime();
    }

    @Override
    public long getSize() {
        if (versionSummary == null) {
            return 0;
        }
        return versionSummary.getSize();
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public VersionOrDeleteMarker getVersionSummary() {
        return versionSummary;
    }

    public void setVersionSummary(VersionOrDeleteMarker versionSummary) {
        this.versionSummary = versionSummary;
    }

    public ObjectMetadata getObjectMetadata() {
        return objectMetadata;
    }

    public void setObjectMetadata(ObjectMetadata objectMetadata) {
        this.objectMetadata = objectMetadata;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }
}
